/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ddnsserver;

/**
 *
 * @author Do Viet Trung
 */
/*
 * BorderLayoutDemo.java is a 1.4 application that requires no other files.
 */

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.BorderFactory;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;
public class DDNSServer implements ActionListener{
    public static boolean RIGHT_TO_LEFT = false;
    private static String newline = "\n";
    private JMenuBar menuBar;
    private JMenu menu, submenu;
    private JMenuItem menuItem;
    private JFrame frame;
    private Container pane;
    private JPanel paneCenter,
            paneLineEnd,
            panePageEnd;
    private JList listUsers,
            listMessage;
    private JTextArea txtMessage;

    private JButton btnListen,
            btnQuit,
            btnStop;
    private SocketServer Server;
    private ServerSocket sockServer;
    private int Port;
    private Vector<String>users;

    
    private JPanel createPageEnd()
    {
        panePageEnd = new JPanel(new FlowLayout(2));
        btnQuit = new JButton("Quit");
        btnQuit.setActionCommand("btnQuit");
        btnQuit.addActionListener(this);
        panePageEnd.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        panePageEnd.add(btnQuit);
        return panePageEnd;
    }
    private JPanel createCenterPane()
    {
        paneCenter = new JPanel(new BorderLayout());
        btnListen = new JButton("Start");
        btnStop = new JButton("Stop");
        btnQuit = new JButton("Quit");
        JPanel paneBtn= new JPanel();
        paneBtn.add(btnListen);
        paneBtn.add(new JLabel("   "));
        paneBtn.add(btnStop);
        paneBtn.add(new JLabel("   "));
        paneBtn.add(btnQuit);
        btnListen.setActionCommand("btnListen");
        btnListen.addActionListener(this);
        btnStop.setActionCommand("btnStop");
        btnStop.addActionListener(this);
        btnQuit.setActionCommand("btnQuit");
        btnQuit.addActionListener(this);

        txtMessage = new JTextArea();
        txtMessage.setEditable(false);
        txtMessage.setBorder(BorderFactory.createLineBorder(Color.lightGray, 2));
        txtMessage.setLineWrap(true);
        txtMessage.setWrapStyleWord(true);
        txtMessage.setAutoscrolls(true);
        JScrollPane myScroll = new JScrollPane(txtMessage);
        txtMessage.setPreferredSize(new Dimension(200, 300));
        addListMessage("Server is Stopped");
        paneCenter.add(new JLabel("Events:"), BorderLayout.PAGE_START);
        paneCenter.add(myScroll, BorderLayout.CENTER);
        paneCenter.add(paneBtn, BorderLayout.PAGE_END);
        paneCenter.setBorder(BorderFactory.createLineBorder(Color.lightGray, 2));
        return paneCenter;
    }

    private JPanel createLineEnd()
    {
        paneLineEnd = new JPanel(new BorderLayout());

        users = new Vector<String>();
        users.add("SERVER");

        listUsers = new JList(users);
        JScrollPane Scpane = new JScrollPane(listUsers);
        paneLineEnd.add(new JLabel("List user Online"), BorderLayout.PAGE_START);
       
        listUsers.setPreferredSize(new Dimension(100 , 200));
        paneLineEnd.add(Scpane, BorderLayout.CENTER);
        paneLineEnd.setBorder(BorderFactory.createLineBorder(Color.lightGray, 2));
        return paneLineEnd;
    }

    public Container addComponentsToPane() {
         pane = frame.getContentPane();
        if (!(pane.getLayout() instanceof BorderLayout)) {
            pane.add(new JLabel("Container doesn't use BorderLayout!"));
            return null;
        }

        if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(
                java.awt.ComponentOrientation.RIGHT_TO_LEFT);
        }

        pane.add(createCenterPane(), BorderLayout.CENTER);
        pane.add(createLineEnd(), BorderLayout.LINE_END);
        //pane.add(createPageEnd(),BorderLayout.PAGE_END);
        Port = 22000;
        return pane;
    }
public JMenuBar createMenuBar() {
        //Create the menu bar.
        menuBar = new JMenuBar();

        //Menu Files
        menu = new JMenu("Tools");
        menu.setMnemonic(KeyEvent.VK_T);
        menu.getAccessibleContext().setAccessibleDescription(
                "All tools of Application");
        menuBar.add(menu);

        //MenuItems: Files -> Changes Attribute
        ImageIcon icon1 = new ImageIcon("images\\ChangeAttribute.gif");
        menuItem = new JMenuItem("DNS Converter",icon1);
        //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_C, ActionEvent.ALT_MASK));
        menuItem.getAccessibleContext().setAccessibleDescription(
                "Convert DNS to BIND");
        menuItem.addActionListener(this);
        menuItem.setEnabled(true);
        menuItem.setActionCommand("Converter");
        menuItem.setName("Converter");
        menu.add(menuItem);

        //MenuItems: Files -> Changes Attribute
        ImageIcon icon2 = new ImageIcon("images\\ChangeAttribute.gif");
        menuItem = new JMenuItem("Option",icon1);
        //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_O, ActionEvent.ALT_MASK));
        menuItem.getAccessibleContext().setAccessibleDescription(
                "Change Option of DDNS");
        menuItem.addActionListener(this);
        menuItem.setEnabled(true);
        menuItem.setActionCommand("Option");
        menuItem.setName("Option");
        menu.add(menuItem);

        ImageIcon icon3 = new ImageIcon("images\\quit.gif");
        menuItem = new JMenuItem("Quit",icon3);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_F4, ActionEvent.ALT_MASK));
        menuItem.setMnemonic(KeyEvent.VK_F4);
        menuItem.addActionListener(this);
        menuItem.setName("Quit");
        menuItem.setActionCommand("MQuit");
        menu.add(menuItem);

        menu = new JMenu("?");
        menu.setMnemonic(KeyEvent.VK_H);
        menu.getAccessibleContext().setAccessibleDescription(
                "About");
        menuItem = new JMenuItem("About");
        menuItem.setName("About");
        menuItem.setActionCommand("MAbout");
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_B, ActionEvent.ALT_MASK));
        menuItem.setMnemonic(KeyEvent.VK_B);
        menuItem.addActionListener(this);
        menu.add(menuItem);

        menuBar.add(menu);
        return menuBar;
    }
 public void actionPerformed(ActionEvent e)
 {
        if(e.getActionCommand() == "Converter")
        {
            DNSConvert DC = new DNSConvert();
            DC.createAndShowGUI();
        }
        if(e.getActionCommand() == "Option")
        {
            String txtNewPort = JOptionPane.showInputDialog(paneCenter, "Listen on new Port", "New Port",3);
        }
        if(e.getActionCommand() == "btnListen")
        {
            btnStop.setEnabled(true);
            btnListen.setEnabled(false);
            try
            {
                Server = new SocketServer(Port, txtMessage, listUsers);
                Server.t.start();
            }catch(Exception ex)
            {
                JOptionPane.showMessageDialog(paneCenter, ex.getMessage(),"DDNS Server - Error",0);
            }
            addListMessage("Server is listening on port " + Port);
            addListMessage("Server load Accounts Database successful");
        }
        if(e.getActionCommand() == "btnStop")
        {
            btnListen.setEnabled(true);
            btnStop.setEnabled(false);
        }
        if(e.getActionCommand() == "btnQuit")
        {
            System.exit(0);
        }
 }
    private void addListMessage(String addMsg)
    {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        String strToday = dateFormat.format(date);
        txtMessage.append("[" + strToday + "]# " + addMsg + newline);
    }

   public void createAndShowGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);
        frame = new JFrame("Dynamic DNS Server");
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Set up the content pane.
        createMenuBar();
        frame.setJMenuBar(menuBar);
        frame.setContentPane(addComponentsToPane());
        frame.setPreferredSize(new Dimension(600, 400));
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);

    }
}

